package cn.dinkevin.xui.widget.button;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;

import cn.dinkevin.xui.R;
import cn.dinkevin.xui.finder.ValueFinder;
import cn.dinkevin.xui.util.LogUtil;

/**
 * Created by ycy on 2018/4/16 0016.
 * 自定义发送验证码计时按钮
 */

@SuppressLint("AppCompatCustomView")
public class CountDownButton extends Button {
    //总时长
    private long millisInFuture;
    //间隔时长
    private long countDownInterval;
    //默认背景颜色
    private int normalColor;
    //倒计时 背景颜色
    private int countDownColor;
    //是否结束
    private boolean isFinish = false;
    //定时器
    private CountDownTimer countDownTimer;
    public CountDownButton(Context context) {
        this(context,null);
    }
    public CountDownButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CountDownButton,defStyleAttr,0);
        //设置默认时长
        millisInFuture = (long) typedArray.getInt(R.styleable.CountDownButton_millisinfuture,60000);
        //设置默认间隔时长
        countDownInterval = (long)typedArray.getInt(R.styleable.CountDownButton_countdowninterva,1000);
        //设置默认背景色
        normalColor = typedArray.getColor(R.styleable.CountDownButton_normalColor, ValueFinder.getColor(R.color.count_button1));
        //设置默认倒计时 背景色
        countDownColor = typedArray.getColor(R.styleable.CountDownButton_countDownColor,ValueFinder.getColor(R.color.count_button2));
        typedArray.recycle();
        //字体居中
        setGravity(Gravity.CENTER);
        setBackground(null);
        //默认文字和背景色
        normalBackground();
        //设置定时器
        countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                LogUtil.e("start");
                //未结束
                isFinish = false;
                setText(String.format(ValueFinder.getString(R.string.send_time),Math.round((double) millisUntilFinished / 1000) - 1));
                setTextColor(isFinish?normalColor:countDownColor);
                setEnabled(isFinish);
            }
            @Override
            public void onFinish() {
                LogUtil.e("start");
                //结束
                isFinish = true;
                normalBackground();
                setEnabled(isFinish);
            }
        };
    }

    private void normalBackground(){
        setText(isFinish?R.string.click_resend:R.string.send_indentify_code);
        setTextColor(isFinish?normalColor:countDownColor);

    }

    public boolean isFinish() {
        return isFinish;
    }

    public void cancel(){
        countDownTimer.cancel();
    }

    public void startCountDownTime(){
        countDownTimer.start();
        LogUtil.e("start");
    }

    public void startOrClose(boolean isFinish){
        this.isFinish = isFinish;
        setTextColor(isFinish?normalColor:countDownColor);
        setText(R.string.send_indentify_code);
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (!isFinish()) {
            cancel();
        }
    }
}